import datetime

from rest_framework import status
from rest_framework.response import Response
from myapp.models import UserModel, OperationLogModel
from utils.force_login import check_login


def operation_log(request, name):
    try:
        # 写入操作模型类
        user = request.session.get('user')
        uid = user['user_id']
        print("------------",uid)
        users = UserModel.objects.filter(id=uid).first()
        print("users",users)
        crea = users.create_time = datetime.datetime.now()  # 更新登录时间
        operation = '%s做了一个操作: %s' % (users.username, name)
        OperationLogModel.objects.create(user_id=uid, operation=operation, router=request.path[7:],times=crea)
    except Exception as e:
        print('添加操作日志的报错信息是', e)
        return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "服务器内部错误"})